
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to deploy with WSGI &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="How to use Django with Apache and mod_wsgi" href="modwsgi.html" />
    <link rel="prev" title="Deploying Django" href="../index.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="../index.html" title="Deploying Django">previous</a>
     |
    <a href="../../index.html" title="“How-to” guides" accesskey="U">up</a>
   |
    <a href="modwsgi.html" title="How to use Django with Apache and &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;mod_wsgi&lt;/span&gt;&lt;/code&gt;">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="howto-deployment-wsgi-index">
            
  <div class="section" id="s-how-to-deploy-with-wsgi">
<span id="how-to-deploy-with-wsgi"></span><h1>How to deploy with WSGI<a class="headerlink" href="#how-to-deploy-with-wsgi" title="Permalink to this headline">¶</a></h1>
<p>Django’s primary deployment platform is <a class="reference external" href="http://www.wsgi.org">WSGI</a>, the Python standard for web
servers and applications.</p>
<p>Django’s <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">startproject</span></code></a> management command sets up a simple default
WSGI configuration for you, which you can tweak as needed for your project,
and direct any WSGI-compliant application server to use.</p>
<p>Django includes getting-started documentation for the following WSGI servers:</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="modwsgi.html">How to use Django with Apache and <code class="docutils literal notranslate"><span class="pre">mod_wsgi</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="apache-auth.html">Authenticating against Django’s user database from Apache</a></li>
<li class="toctree-l1"><a class="reference internal" href="gunicorn.html">How to use Django with Gunicorn</a></li>
<li class="toctree-l1"><a class="reference internal" href="uwsgi.html">How to use Django with uWSGI</a></li>
</ul>
</div>
<div class="section" id="s-the-application-object">
<span id="the-application-object"></span><h2>The <code class="docutils literal notranslate"><span class="pre">application</span></code> object<a class="headerlink" href="#the-application-object" title="Permalink to this headline">¶</a></h2>
<p>The key concept of deploying with WSGI is the <code class="docutils literal notranslate"><span class="pre">application</span></code> callable which
the application server uses to communicate with your code. It’s commonly
provided as an object named <code class="docutils literal notranslate"><span class="pre">application</span></code> in a Python module accessible to
the server.</p>
<p>The <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">startproject</span></code></a> command creates a file
<code class="file docutils literal notranslate"><span class="pre">&lt;project_name&gt;/wsgi.py</span></code> that contains such an <code class="docutils literal notranslate"><span class="pre">application</span></code> callable.</p>
<p>It’s used both by Django’s development server and in production WSGI
deployments.</p>
<p>WSGI servers obtain the path to the <code class="docutils literal notranslate"><span class="pre">application</span></code> callable from their
configuration. Django’s built-in server, namely the <a class="reference internal" href="../../../ref/django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">runserver</span></code></a>
command, reads it from the <a class="reference internal" href="../../../ref/settings.html#std:setting-WSGI_APPLICATION"><code class="xref std std-setting docutils literal notranslate"><span class="pre">WSGI_APPLICATION</span></code></a> setting. By default, it’s
set to <code class="docutils literal notranslate"><span class="pre">&lt;project_name&gt;.wsgi.application</span></code>, which points to the <code class="docutils literal notranslate"><span class="pre">application</span></code>
callable in <code class="file docutils literal notranslate"><span class="pre">&lt;project_name&gt;/wsgi.py</span></code>.</p>
</div>
<div class="section" id="s-configuring-the-settings-module">
<span id="configuring-the-settings-module"></span><h2>Configuring the settings module<a class="headerlink" href="#configuring-the-settings-module" title="Permalink to this headline">¶</a></h2>
<p>When the WSGI server loads your application, Django needs to import the
settings module — that’s where your entire application is defined.</p>
<p>Django uses the <span class="target" id="index-0"></span><a class="reference internal" href="../../../topics/settings.html#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> environment variable to
locate the appropriate settings module. It must contain the dotted path to the
settings module. You can use a different value for development and production;
it all depends on how you organize your settings.</p>
<p>If this variable isn’t set, the default <code class="file docutils literal notranslate"><span class="pre">wsgi.py</span></code> sets it to
<code class="docutils literal notranslate"><span class="pre">mysite.settings</span></code>, where <code class="docutils literal notranslate"><span class="pre">mysite</span></code> is the name of your project. That’s how
<a class="reference internal" href="../../../ref/django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">runserver</span></code></a> discovers the default settings file by default.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Since environment variables are process-wide, this doesn’t work when you
run multiple Django sites in the same process. This happens with mod_wsgi.</p>
<p class="last">To avoid this problem, use mod_wsgi’s daemon mode with each site in its
own daemon process, or override the value from the environment by
enforcing <code class="docutils literal notranslate"><span class="pre">os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;]</span> <span class="pre">=</span> <span class="pre">&quot;mysite.settings&quot;</span></code> in
your <code class="file docutils literal notranslate"><span class="pre">wsgi.py</span></code>.</p>
</div>
</div>
<div class="section" id="s-applying-wsgi-middleware">
<span id="applying-wsgi-middleware"></span><h2>Applying WSGI middleware<a class="headerlink" href="#applying-wsgi-middleware" title="Permalink to this headline">¶</a></h2>
<p>To apply <a class="reference external" href="https://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides">WSGI middleware</a> you can simply wrap the application object. For
instance you could add these lines at the bottom of <code class="file docutils literal notranslate"><span class="pre">wsgi.py</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">helloworld.wsgi</span> <span class="k">import</span> <span class="n">HelloWorldApplication</span>
<span class="n">application</span> <span class="o">=</span> <span class="n">HelloWorldApplication</span><span class="p">(</span><span class="n">application</span><span class="p">)</span>
</pre></div>
</div>
<p>You could also replace the Django WSGI application with a custom WSGI
application that later delegates to the Django WSGI application, if you want
to combine a Django application with a WSGI application of another framework.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">How to deploy with WSGI</a><ul>
<li><a class="reference internal" href="#the-application-object">The <code class="docutils literal notranslate"><span class="pre">application</span></code> object</a></li>
<li><a class="reference internal" href="#configuring-the-settings-module">Configuring the settings module</a></li>
<li><a class="reference internal" href="#applying-wsgi-middleware">Applying WSGI middleware</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../index.html"
                        title="previous chapter">Deploying Django</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="modwsgi.html"
                        title="next chapter">How to use Django with Apache and <code class="docutils literal notranslate"><span class="pre">mod_wsgi</span></code></a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/howto/deployment/wsgi/index.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="../index.html" title="Deploying Django">previous</a>
     |
    <a href="../../index.html" title="“How-to” guides" accesskey="U">up</a>
   |
    <a href="modwsgi.html" title="How to use Django with Apache and &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;mod_wsgi&lt;/span&gt;&lt;/code&gt;">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>